package com.blueocean_health.care.management.service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.blueocean_health.care.management.em.OrderFlagEnum;

import org.apache.poi.ss.util.SSCellRange;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.blueocean_health.care.management.common.base.em.TimeType;
import com.blueocean_health.care.management.common.base.time.TimeUtils;
import com.blueocean_health.care.management.domain.base.Homestatisticbyteam;
import com.blueocean_health.care.management.domain.base.Homestatistics;
import com.blueocean_health.care.management.domain.base.Homestatisticsall;
import com.blueocean_health.care.management.domain.base.OrderInfo;
import com.blueocean_health.care.management.domain.base.Selectzhdzlist;
import com.blueocean_health.care.management.domain.ordervo.SelectZHDZVo;
import com.blueocean_health.care.management.em.OrderStatusEnum;
import com.blueocean_health.care.management.em.PayStatusEnum;
import com.blueocean_health.care.management.mapper.HomestatisticbyteamMapper;
import com.blueocean_health.care.management.mapper.HomestatisticsMapper;
import com.blueocean_health.care.management.mapper.HomestatisticsallMapper;

/**
 * 定时任务改变订单状态
 * @Author ChenPeng cp
 */
@Service
public class ScheduleHome {

	@Autowired
	private HomestatisticsMapper homestatisticsMapper;
	@Autowired
	private HomestatisticsallMapper homestatisticsallMapper;
	@Autowired
	private HomestatisticbyteamMapper homestatisticbyteamMapper;
//	  @Value("${sys.schedule}")
//		private boolean schedule;
	//使用cron表达式
	//@Scheduled(cron=" 0 0/30 * * * *")//每隔30分钟执行一次
	public void schedule(){
		long currentTimeMillis1 = System.currentTimeMillis();
	    Date date=new Date();
	    Date date1=TimeUtils.getDateByString("2017-09-01", TimeType.yyyy_MM_dd);
	    long nd = 1000 * 24 * 60 * 60;
	    while(date.getTime()>date1.getTime()){
		String stringByDate = TimeUtils.getStringByDate(date, TimeType.yyyy_MM_dd);
		List<Homestatistics> homelist = homestatisticsMapper.homelist(stringByDate);
		List<Homestatisticsall> homestatisticsall=new  ArrayList<Homestatisticsall>();
		  for (Homestatistics homestatistics : homelist) {
			  Homestatisticsall homestatisticsalls=new Homestatisticsall();
			  BeanUtils.copyProperties(homestatistics, homestatisticsalls);
			  toCaiPrice(homestatisticsalls,date);
			  tototalPay(homestatisticsalls, date);
			  homestatisticsall.add(homestatisticsalls);
		}
		  Map<String, List<Homestatisticsall>> collect = homestatisticsall.stream().collect(Collectors.groupingBy(h->h.getHospitalId()));
		  List<Homestatisticbyteam> homestatisticbyteamList=new ArrayList<Homestatisticbyteam>();
		  collect.forEach((k,v)->
		  {
			  Homestatisticbyteam homestatisticbyteam=new Homestatisticbyteam();
			  //项目id
			  String hospitalId=k;
			  //患者人数集合
			  List<String> parentList = v.stream().map(p -> p.getpNamePhone()).distinct().collect(Collectors.toList());
			  String workString = v.stream().map(s -> s.getServNurseIds()).collect(Collectors.joining(","));
			  List<String> workList = Arrays.asList (workString.split(",")).stream().distinct().collect(Collectors.toList());;
			  double sumCaiJianPrice = v.stream().mapToDouble(p -> p.getCaiJianPrice()).sum();
			  double sumCaiJianPay = v.stream().mapToDouble(p -> p.getCaiJianPay()).sum();
			  double sumtotalprice=0.0;
			 for (Homestatisticsall m : v) {
				  Date billingTime = m.getBillingTime();
				  if(null!=billingTime){
					  String strbilling = TimeUtils.getStringByDate(billingTime, TimeType.yyyy_MM_dd);
					  if(m.getPayStatus().equals(PayStatusEnum._2.getKey())&&strbilling.equals(stringByDate)){
						  sumtotalprice+=m.getActualTotalPrice();
					  }
				  }
			 }
			 homestatisticbyteam.setHospitalId(hospitalId);
			 homestatisticbyteam.setParentCount(parentList.size());
			 homestatisticbyteam.setWorkCount(workList.size());
			 String parentLists = String.join(",", parentList);
			 String workLists = String.join(",", workList);
			 homestatisticbyteam.setStatisticDate(TimeUtils.getDateByString(stringByDate, TimeType.yyyy_MM_dd));
			 homestatisticbyteam.setParentList(parentLists);
			 homestatisticbyteam.setWorkList(workLists);
			 homestatisticbyteam.setSumCaiJianPay(sumCaiJianPay);
			 homestatisticbyteam.setSumCaiJianPrice(sumCaiJianPrice);
			 homestatisticbyteam.setSumTotalPrice(sumtotalprice);
			 homestatisticbyteamList.add(homestatisticbyteam);
		  System.out.println("Item : " + k + " Count : " + v);
		  });
		  homestatisticbyteamMapper.insertList(homestatisticbyteamList);
		 homestatisticsallMapper.insertList(homestatisticsall);
		  date=new Date(date.getTime()-nd);
		  }
	    long currentTimeMillis2 = System.currentTimeMillis();
		System.out.println("请求耗时:::"+(currentTimeMillis2-currentTimeMillis1)+"毫秒");
		
	}
	
	private Homestatisticsall toCaiPrice(Homestatisticsall homestatistics,Date stringByDate){
		 long nd = 1000 * 24 * 60 * 60;
		Integer caiDay = homestatistics.getCaiDay()==null?0:homestatistics.getCaiDay();
		 Integer caiHour = homestatistics.getCaiHour()==null?0:homestatistics.getCaiHour();
		Double actualDayPrice = homestatistics.getActualDayPrice()==null?0:homestatistics.getActualDayPrice();
		Double oneHourPrice = homestatistics.getOneHourPrice()==null?0:homestatistics.getOneHourPrice();
		Double actualTotalPrice = homestatistics.getActualTotalPrice()==null?0:homestatistics.getActualTotalPrice();
		Double caiPrice= (caiDay*actualDayPrice)+(caiHour*oneHourPrice);
		//筛选开始时间
		//Date serviceBeginTimeByString = TimeUtils.getDateByString(selectZHDZVo.getServiceBeginTime(),TimeType.yyyy_MM_dd_HH_mm_ss);
		//Date serviceEndTimeByString = TimeUtils.getDateByString(selectZHDZVo.getServiceEndTime(),TimeType.yyyy_MM_dd_HH_mm_ss);
		//订单开始时间
		Date beginTimes = homestatistics.getBeginTimes();
		Date endTimes = homestatistics.getEndTimes();
		//当订单的开始时间小于查询条件的开始时间，订单的服务结束时间小于查询条件的结束时间 减法
		if(beginTimes.getTime()<stringByDate.getTime()&&endTimes.getTime()<=stringByDate.getTime()+1*nd){
			 int day = (int) ((stringByDate.getTime() -beginTimes.getTime())/nd);
			 double oldprice = day*actualDayPrice;
			 BigDecimal actualTotalPriceb = new BigDecimal(Double.toString(actualTotalPrice));
			 BigDecimal oldpriceb = new BigDecimal(Double.toString(oldprice));
			caiPrice=actualTotalPriceb.subtract(oldpriceb).doubleValue();
		}
		//当订单的开始时间大于查询条件的开始时间，订单的服务结束时间小于查询条件的结束时间 直接取
		if(beginTimes.getTime()>=stringByDate.getTime()&&endTimes.getTime()<=stringByDate.getTime()+1*nd){
		
			caiPrice=actualTotalPrice;
		}
		homestatistics.setCaiJianPrice(caiPrice);
		 return homestatistics;
		
		
	}
	
	private static Homestatisticsall tototalPay(Homestatisticsall homestatistics,Date stringByDate){
		 long nd = 1000 * 24 * 60 * 60;
		Integer caiDay = homestatistics.getCaiDay()==null?0:homestatistics.getCaiDay();
		 Integer caiHour = homestatistics.getCaiHour()==null?0:homestatistics.getCaiHour();
		Double totalPay = homestatistics.getTotalPay()==null?0:homestatistics.getTotalPay();
		 Double oneHourPay = homestatistics.getOneHourPay()==null?0:homestatistics.getOneHourPay();
		 Double oneDayPay = homestatistics.getOneDayPay()==null?0:homestatistics.getOneDayPay();
		Double caipay= (caiDay*oneDayPay)+(caiHour*oneHourPay);
		//筛选开始时间
		//Date serviceBeginTimeByString = TimeUtils.getDateByString(selectZHDZVo.getServiceBeginTime(),TimeType.yyyy_MM_dd_HH_mm_ss);
		//Date serviceEndTimeByString = TimeUtils.getDateByString(selectZHDZVo.getServiceEndTime(),TimeType.yyyy_MM_dd_HH_mm_ss);
		//订单开始时间
		Date beginTimes = homestatistics.getBeginTimes();
		Date endTimes = homestatistics.getEndTimes();
		//当订单的开始时间小于查询条件的开始时间，订单的服务结束时间小于查询条件的结束时间 减法
		if(beginTimes.getTime()<stringByDate.getTime()&&endTimes.getTime()<=stringByDate.getTime()+1*nd){
			 int day = (int) ((stringByDate.getTime() -beginTimes.getTime())/nd);
			 double oldpay = day*oneDayPay;
			 BigDecimal actualTotalPayb = new BigDecimal(Double.toString(totalPay));
			 BigDecimal oldpayb = new BigDecimal(Double.toString(oldpay));
			 caipay=actualTotalPayb.subtract(oldpayb).doubleValue();
		}
		//当订单的开始时间大于查询条件的开始时间，订单的服务结束时间小于查询条件的结束时间 直接取
		if(beginTimes.getTime()>=stringByDate.getTime()&&endTimes.getTime()<=stringByDate.getTime()+1*nd){
		
			caipay=totalPay;
		}
		homestatistics.setCaiJianPay(caipay);
		 return homestatistics;
		
		
	}
}
